package singletontest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 枚举enum 和静态代码块的特性相似，在使用枚举类时，构造方法会被自动调用，也可以应用其这个特性实现单例设计模式。
 *
 * @author liuziyang
 * @create 2019-10-14 20:52
 */
public enum SingletonTest10 {
    INSTANCE;
    private MyClass myObject = null;

    SingletonTest10() {
        myObject = new MyClass();
    }

    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest10.INSTANCE.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest10.INSTANCE.getInstance().hashCode());
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(SingletonTest10.INSTANCE.getInstance().hashCode());
            }
        }).start();
    }

    public MyClass getInstance() {
        return myObject;
    }
}

class MyClass {

}